syntax enable
" 鼠标可以移动，调整窗口等
set mouse=a
" 超过 window 宽度的行不要折叠
set nowrap
" 自动进入到新打开的窗口
set splitbelow
set splitright
" 打开行号
set number
" 设置行号宽度
set numberwidth=5
" 高亮光标所在行
set cursorline
set termguicolors
" 因为失去焦点就会自动保存，所以没有必要使用 swapfile
set noswapfile
" 自动隐藏 command-line
set cmdheight=0
" 让退出 vim 之后 undo 消息不消失
set undofile
" 只有一个全局的 status line，而不是每一个 window 一个
set laststatus=3
" 当打开文件的时候，自动进入到上一次编辑的位置
lua vim.api.nvim_create_autocmd( "BufReadPost", { command = [[if line("'\"") > 1 && line("'\"") <= line("$") | execute "normal! g`\"" | endif]] })
" 当文件被其他编辑器修改时，自动加载
set autoread
au FocusGained,BufEnter * :checktime
" 当失去焦点或者离开当前的 buffer 的时候保存
set autowrite
autocmd FocusLost,BufLeave * silent! update
" 在 terminal 中也是使用 esc 来进入 normal 模式
tnoremap  <Esc>  <C-\><C-n>
" 映射 leader 键为 ,
let g:mapleader = ','
" 将 q 映射为 <leader>q，因为录制宏的操作比较少，而关掉窗口的操作非常频繁
" noremap <leader>q q
" 设置保存键
noremap <C-s> :w<cr>
" 插入模式中保存自动并退出插入模式
inoremap <C-s> <ESC>:w<cr>

" 访问系统剪切板
map <leader>y "+y
map <leader>p "+p
map <leader>d "+d

" 让远程的 server 内容拷贝到系统剪切板中，具体参考 https://github.com/ojroques/vim-oscyank
autocmd TextYankPost * if v:event.operator is 'y' && v:event.regname is '+' | execute 'OSCYankReg +' | endif
autocmd TextYankPost * if v:event.operator is 'd' && v:event.regname is '+' | execute 'OSCYankReg +' | endif

" 使用 z a 打开和关闭 fold
set foldlevelstart=99
set foldmethod=expr
set foldexpr=nvim_treesitter#foldexpr()

" 加载 lua 配置
lua require 'usr.load_plugin'

" 加载 vim 配置, 参考 https://github.com/jdhao/nvim-config
let s:core_conf_files = [
      \ 'misc.vim',
      \ 'coc.vim',
      \ 'debug.vim',
      \ 'wilder.vim',
      \ 'startify.vim',
      \ ]

for s:fname in s:core_conf_files
  execute printf('source %s/vim/%s', stdpath('config'), s:fname)
endfor

" keymapping by whichkey doesn't work in neovim 0.8
noremap <Space>bc :BDelete hidden<cr>

" 因为 nvim-treesitter-textobjects 使用 x 来跳转，原始的 x 被映射为 xx
nn xx x

" don't show the help in normal mode
" let g:Lf_HideHelp = 1
" let g:Lf_UseCache = 0
" let g:Lf_UseVersionControlTool = 0
" let g:Lf_IgnoreCurrentBufferName = 1
" let g:Lf_PreviewCode=1
" " popup mode
" let g:Lf_WindowPosition = 'popup'
" let g:Lf_PreviewInPopup = 1
" let g:Lf_StlSeparator = { 'left': "\ue0b0", 'right': "\ue0b2", 'font': "DejaVu Sans Mono for Powerline" }
" let g:Lf_PreviewResult = {'Function': 1, 'BufTag': 1 }

" let g:Lf_ShortcutF = "<leader>ff"
" noremap <leader>fb :<C-U><C-R>=printf("Leaderf buffer %s", "")<CR><CR>
" noremap <leader>fm :<C-U><C-R>=printf("Leaderf mru %s", "")<CR><CR>
" noremap <leader>ft :<C-U><C-R>=printf("Leaderf bufTag %s", "")<CR><CR>
" noremap <leader>fl :<C-U><C-R>=printf("Leaderf line %s", "")<CR><CR>
" noremap <leader>fs :<C-U><C-R>=printf("LeaderfFunctionAll %s", "")<CR><CR>

" 设置tab为4个空格
set tabstop=4       " 设置tab为4个空格
set softtabstop=4   " 设置编辑模式按退格时退回的空格长度
set shiftwidth=4    " 每一级的缩进长度
set expandtab       " 设置缩进用空格来表示
set autoindent      " 自动缩进
set relativenumber  " 设置相对行号

" 重映射winresizer快捷键
let g:winresizer_start_key = '<Space>e'
let g:floaterm_keymap_toggle = '<C-a>t'

" 顶部和底部总有5行
set scrolloff=5

" 创建自动命令, 在创建新的窗口时自动行尾空格高亮设置
highlight TrailingWhitespace ctermbg=red guibg=red
call matchadd("TrailingWhitespace", '\v\s+$') " 手动调用的原因是第一个窗口需要匹配
autocmd WinNew * silent! call matchadd("TrailingWhitespace", '\v\s+$')

" 彩色括号插件开启标志
let g:rainbow_active = 1

" 设置当前光标高亮
hi CursorColumn ctermbg=DarkGray ctermfg=DarkCyan guibg=DarkGray guifg=DarkCyan
" 设置匹配括号高亮
hi MatchParen ctermbg=Yellow guibg=Yellow
